package com.easyOrder.core.push;

import com.easyOrder.core.exception.api.HttpClientException;
import com.easyOrder.core.utils.HttpObjectRequestClient;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.log4j.Logger;

import java.util.Date;
import java.util.Map;

public class UmengDelivery {
	
	private static final String URI = "http://msg.umeng.com/api/send";

	private String appKey;

	private String appMasterSecret;

	private String validationTokenPrefix;
	
	private static final Logger logger = Logger.getLogger(UmengDelivery.class);

	public UmengDelivery() {
	}

	public String getAppKey() {
		return appKey;
	}

	public void setAppKey(String value) {
		appKey = value;
		ensureValidationTokenPrefix();
	}

	public String getAppMasterSecret() {
		return appMasterSecret;
	}

	public void setAppMasterSecret(String value) {
		appMasterSecret = value;
		ensureValidationTokenPrefix();
	}

	public void send(Notification message) {
		StringBuilder deviceCodes = new StringBuilder();
		for (int i = 0; i < message.getRecipients().size(); i++) {
			if (deviceCodes.length() != 0) {
				deviceCodes.append(',');
			}
			deviceCodes.append(message.getRecipients().get(i).getCode());
		}
		UmengMessage umengMessage = new UmengMessage();
		umengMessage.setAppkey(appKey);
		umengMessage.setTimestamp(String.valueOf(new Date().getTime()));
		umengMessage.setValidation_token(DigestUtils
				.md5Hex(validationTokenPrefix + umengMessage.getTimestamp()));

		umengMessage.setDevice_tokens(deviceCodes.toString());
		UmengMessageBody umengMessageBody = umengMessage.getPayload().getBody();
		umengMessageBody.setTitle(message.getTitle());
		umengMessageBody.setText(message.getContent());
		umengMessageBody.setCustom(message.getSubjectId());

		HttpObjectRequestClient httpClient = new HttpObjectRequestClient();
		Object reponse = null;
		try {
			reponse = httpClient.execute(URI, "POST", umengMessage);
		} catch (HttpClientException e) {
			logger.error(e, e);
		}
		if ((reponse != null)
				&& ((Map<?, ?>) reponse).get("ret").equals("FAIL")) {
			System.out.println("推送 Umeng 消息失败");
		}
	}

	private void ensureValidationTokenPrefix() {
		if (appKey == null) {
			validationTokenPrefix = appMasterSecret;
		} else if (appMasterSecret == null) {
			validationTokenPrefix = appKey;
		} else {
			validationTokenPrefix = appKey + appMasterSecret;
		}
	}

	class UmengMessage {
		private String appkey;

		private String timestamp;

		private String validation_token;

		private String type = "listcast";

		private String device_tokens;

		private UMengMessagePayload payload;

		public UmengMessage() {
			payload = new UMengMessagePayload();
			payload.body = new UmengMessageBody();
		}

		public String getAppkey() {
			return appkey;
		}

		public void setAppkey(String appkey) {
			this.appkey = appkey;
		}

		public String getTimestamp() {
			return timestamp;
		}

		public void setTimestamp(String timestamp) {
			this.timestamp = timestamp;
		}

		public String getValidation_token() {
			return validation_token;
		}

		public void setValidation_token(String validation_token) {
			this.validation_token = validation_token;
		}

		public String getType() {
			return type;
		}

		public void setType(String type) {
			this.type = type;
		}

		public String getDevice_tokens() {
			return device_tokens;
		}

		public void setDevice_tokens(String device_tokens) {
			this.device_tokens = device_tokens;
		}

		public UMengMessagePayload getPayload() {
			return payload;
		}

		public void setPayload(UMengMessagePayload payload) {
			this.payload = payload;
		}
	}

	class UMengMessagePayload {
		private String display_type = "notification";

		private UmengMessageBody body;

		public String getDisplay_type() {
			return display_type;
		}

		public void setDisplay_type(String display_type) {
			this.display_type = display_type;
		}

		public UmengMessageBody getBody() {
			return body;
		}

		public void setBody(UmengMessageBody body) {
			this.body = body;
		}
	}

	class UmengMessageBody {
		private String title;

		private String text;

		private String after_open = "custom";

		private String custom;

		public String getTitle() {
			return title;
		}

		public void setTitle(String title) {
			this.title = title;
		}

		public String getTicker() {
			return title;
		}

		public void setTicker(String ticker) {
			this.title = ticker;
		}

		public String getText() {
			return text;
		}

		public void setText(String text) {
			this.text = text;
		}

		public String getAfter_open() {
			return after_open;
		}

		public void setAfter_open(String after_open) {
			this.after_open = after_open;
		}

		public String getCustom() {
			return custom;
		}

		public void setCustom(String custom) {
			this.custom = custom;
		}
	}
}
